Ext.layout.TableFormLayout = Ext.extend(Ext.layout.TableLayout, {
	
			clearTableRows:function(){
				if(this.table){
					var rows=this.table.tBodies[0].childNodes;
					while(rows.length>0){
						this.table.tBodies[0].removeChild(rows[0]);
					}
					this.currentColumn=0;
					this.currentRow=0;
					this.cells=[];
				}
			},
			getLabelStyle : function(s) {
				var ls = '', items = [this.labelStyle, s];
				for (var i = 0, len = items.length; i < len; ++i) {
					if (items[i]) {
						ls += items[i];
						if (ls.substr(-1, 1) != ';') {
							ls += ';'
						}
					}
				}
				return ls;
			},
			// private
			isValidParent : function(c, target) {
				return target
						&& this.container.getEl().contains(c.getPositionEl());
			},
			getTemplateArgs : function(field) {
				var noLabelSep = !field.fieldLabel || field.hideLabel;
				return {
					id : field.id,
					label : field.fieldLabel,
					labelStyle : this.getLabelStyle(field.labelStyle),
					elementStyle : this.elementStyle || '',
					labelSeparator : noLabelSep ? '' : (Ext
							.isDefined(field.labelSeparator)
							? field.labelSeparator
							: this.labelSeparator),
					itemCls : (field.itemCls || this.container.itemCls || '')
							+ (field.hideLabel ? ' x-hide-label' : ''),
					clearCls : field.clearCls || 'x-form-clear-left'
				};
			},
			setContainer : function(ct) {
				Ext.layout.TableFormLayout.superclass.setContainer.call(this,ct);
				if (ct.labelAlign) {
					ct.addClass('x-form-label-' + ct.labelAlign);
				}

				if (ct.hideLabels) {
					this.labelStyle = "display:none";
					this.elementStyle = "padding-left:0;";
					this.labelAdjust = 0;
				} else {
					this.labelSeparator = ct.labelSeparator
							|| this.labelSeparator;
					ct.labelWidth = ct.labelWidth || 100;
					if (typeof ct.labelWidth == 'number') {
						var pad = (typeof ct.labelPad == 'number'
								? ct.labelPad
								: 5);
						this.labelAdjust = ct.labelWidth + pad;
						this.labelStyle = "width:" + ct.labelWidth + "px;";
						this.elementStyle = "padding-left:"
								+ (ct.labelWidth + pad) + 'px';
					}
					if (ct.labelAlign == 'top') {
						this.labelStyle = "width:auto;";
						this.labelAdjust = 0;
						this.elementStyle = "padding-left:0;";
					}
				}
			},
 // private
    
		renderItem : function(c, position, target) {
				if (c && !c.rendered) {
					
					this.formRenderItem(c, position,Ext.get(this.getNextCell(c)));
				}
			},
		 formRenderItem : function(c, position, target){
		 	      c.container = Ext.get(target);
		 	      c.removeMode = 'container';
		        if(c && (c.isFormField || c.fieldLabel) && c.inputType != 'hidden'){
		        	 
		            var args = this.getTemplateArgs(c);
		            if(Ext.isNumber(position)){
		                position = target.dom.childNodes[position] || null;
		            }
		            if(position){
		                c.itemCt = this.fieldTpl.insertBefore(position, args, true);
		            }else{
		                c.itemCt = this.fieldTpl.append(target, args, true);
		            }
		            if(!c.getItemCt){
		                
		                Ext.apply(c, {
		                    getItemCt: function(){
		                        return c.itemCt;
		                    },
		                    customItemCt: true
		                });
		            }
		            c.label = c.getItemCt().child('label.x-form-item-label');
		            if(!c.rendered){
		                c.render('x-form-el-' + c.id);
		            }else if(!this.isValidParent(c, target)){
		                Ext.fly('x-form-el-' + c.id).appendChild(c.getPositionEl());
		            }
		            if(this.trackLabels){
		                if(c.hidden){
		                    this.onFieldHide(c);
		                }
		                c.on({
		                    scope: this,
		                    show: this.onFieldShow,
		                    hide: this.onFieldHide
		                });
		            }
		            this.configureItem(c);
		             c.container = Ext.get(target);
		          
		        }else {
		            Ext.layout.FormLayout.superclass.renderItem.apply(this, arguments);
		        }
		    },

			getRow : function(index) {
				var row = this.table.tBodies[0].childNodes[index];
				if (!row) {
					row = this.createRow();
					this.table.tBodies[0].appendChild(row);
				}
				return row;
			},

			createCell : function(colspan, rowspan, cellCls) {
				var cls = 'x-table-layout-cell';
				var td = document.createElement('td');
				if (colspan) {
					td.colSpan = colspan;
				}
				if (rowspan) {
					td.rowSpan = colspan;
				}
				if (cellCls) {
					cls += ' ' + c.cellCls;
				}
				td.className = cls;
				return td;
			},

			createRow : function() {
				var row = document.createElement('tr');
				row.className = "x-table-row";
				return row;
			},

			insertRow : function(index, items) {
				var oldTr = Ext.get(this.table.tBodies[0].childNodes[index]);
				var newTr = Ext.get(this.createRow());
				newTr.insertAfter(oldTr);
				if (items && items.length && items.length > 0) {
					for (var i = 0, j = items.length; i < j; i++) {
						var t = items[i];
						var td = this.createCell(t.colspan, t.rowspan,t.cellCls);
						newTr.appendChild(td);
						Ext.layout.FormLayout.prototype.renderItem.call(this,t, 0, Ext.get(td));
					}
				}
			}
		});
Ext.Container.LAYOUTS['tableform'] = Ext.layout.TableFormLayout;
